package com.drerp.RH.carreira.DAO;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.drerp.RH.Cargo;
import com.drerp.RH.Colaborador;
import com.drerp.RH.DAO.GenericoDAO;
import com.drerp.RH.carreira.RegraPlanoCarreira;

public class RegraPlanoCarreiraDAO extends GenericoDAO<RegraPlanoCarreira, Long> {
	
	public List<RegraPlanoCarreira> encontraRegraPorCargo(Cargo cargo) {
		List<RegraPlanoCarreira> list = null;

		Transaction trans = null;
		Session hibernateSession = super.getSession();

		try {
			trans = hibernateSession.beginTransaction();
			
			String hql = "SELECT a FROM RegraPlanoCarreira a WHERE a.cargoAlvo = :cargo";
			Query query = hibernateSession.createQuery(hql).setParameter("cargo", cargo);
			list = super.findMany(query);
			
			trans.commit();
		} catch (RuntimeException e) {
			if (trans != null) {
				trans.rollback();
			}

			e.printStackTrace();
		}
		
		return list;
	}
	
	public List<RegraPlanoCarreira> encontraRegraPorColaborador(Colaborador colaborador) {
		List<RegraPlanoCarreira> list = null;

		Transaction trans = null;
		Session hibernateSession = super.getSession();

		try {
			trans = hibernateSession.beginTransaction();
			
			String hql = "SELECT a FROM RegraPlanoCarreira a WHERE a.colaboradorAlvo = :colaborador";
			Query query = hibernateSession.createQuery(hql).setParameter("colaborador", colaborador);
			list = super.findMany(query);
			
			trans.commit();
		} catch (RuntimeException e) {
			if (trans != null) {
				trans.rollback();
			}

			e.printStackTrace();
		}
		
		return list;
	}
	
	public List<RegraPlanoCarreira> encontraRegraParaTodos() {
		List<RegraPlanoCarreira> list = null;

		Transaction trans = null;
		Session hibernateSession = super.getSession();

		try {
			trans = hibernateSession.beginTransaction();
			
			String hql = "SELECT a FROM RegraPlanoCarreira a WHERE a.colaboradorAlvo is null and a.cargoAlvo is null";
			Query query = hibernateSession.createQuery(hql);
			list = super.findMany(query);
			
			trans.commit();
		} catch (RuntimeException e) {
			if (trans != null) {
				trans.rollback();
			}

			e.printStackTrace();
		}
		
		return list;
	}
	
}
